<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns="http://esrc.unimelb.edu.au/OHRM#"
   xmlns:rdfs="http://www.w3.org/TR/WD-rdf-schema#"
   xmlns:fn="http://www.w3.org/2005/xpath-functions"
   version="2.0">

   <!-- author: Cyrus Keong-->
   <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
   <xsl:template match="/database">
      <rdf:RDF>
         <xsl:for-each select="table">

            <xsl:if test="@name='ENTITY'">
               <xsl:call-template name="handleENTITY"/>
            </xsl:if>
            <!-- EPRRSHIP table, 124 records  -->
            <!-- PUBRESOURCE table, 458 records, publication -->
            <xsl:if test="@name='PUBRESOURCE'">
               <xsl:call-template name="handlePUBRESOURCE"/>
            </xsl:if>

            <!-- EFRSHIP table, 842 records, for assigning ANZSRC FOR code to individual ENTITY -->
            <!-- FUNCTION table, 175 records, ANZSRC FOR classification  http://www.abs.gov.au/Ausstats/abs@.nsf/Latestproducts/6BB427AB9696C225CA2574180004463E?opendocument-->
            <xsl:if test="@name='FUNCTION'">
               <xsl:call-template name="handleFUNCTION"/>
            </xsl:if>

            <!-- RELATEDENTITY table, 4672 records -->

            <!-- ENTITYNAME table, 542 records, storing acronym, ANDS ID, ARC ID, Trove ID associated with projects/ people/ organizations-->
            <xsl:if test="@name='ENTITYNAME'">
               <xsl:call-template name="handleENTITYNAME"/>
            </xsl:if>

            <!-- RELATEDRESOURCE not yet known the purpose-->


            <!-- RELATIONSHIPS, 46 records-->
            <xsl:if test="@name='RELATIONSHIPS'">
               <xsl:call-template name="handleRELATIONSHIPS"/>
            </xsl:if>


         </xsl:for-each>
      </rdf:RDF>
   </xsl:template>



   <!--
        handle OHRM's ENTITY table
    -->
   <xsl:template name="handleENTITY">
      <xsl:if test="@name='ENTITY'">
         <xsl:for-each select="row">



            <xsl:element name="rdf:Description">

               <xsl:variable name="eid" select="field[@name='EID']/text()"/>
               <xsl:call-template name="createRDFDescriptionAbout">
                  <xsl:with-param name="about" select="$eid"/>
               </xsl:call-template>

               <xsl:variable name="eType" select="field[@name='ETYPE']/text()"/>
               <xsl:call-template name="createEntityType">
                  <xsl:with-param name="entityType" select="$eType"/>
               </xsl:call-template>

               <xsl:for-each select="field">

                  <xsl:call-template name="genericCopyFromTable"/>

               </xsl:for-each>

               <!-- try to find out all assoicated entity ID -->
               <xsl:call-template name="createEntityName">
                  <xsl:with-param name="entityId" select="$eid"/>
               </xsl:call-template>

               <!-- try to find out all assoicated ANSRC for-->
               <xsl:call-template name="handleOHRMFunction">
                  <xsl:with-param name="entityId" select="$eid"/>
               </xsl:call-template>

               <xsl:call-template name="handlePublication">
                  <xsl:with-param name="entityId" select="$eid"/>
               </xsl:call-template>

               <xsl:call-template name="createRelatedEntity">
                  <xsl:with-param name="entityId" select="$eid"/>
               </xsl:call-template>

            </xsl:element>

         </xsl:for-each>
      </xsl:if>

   </xsl:template>


   <!-- -->
   <xsl:template name="createEntityType">
      <xsl:param name="entityType"/>
      <xsl:element name="rdf:type">
         <xsl:attribute name="rdf:resource">
            <xsl:value-of select="translate($entityType,'','_')"/>
         </xsl:attribute>
      </xsl:element>
      <!-- rdfs:subClassOf rdf:resource="&rdfs;Resource"/-->
      <xsl:element name="rdfs:subClassOf">
         <xsl:attribute name="rdf:resource">OHRM:ENTITY</xsl:attribute>
      </xsl:element>
      <xsl:element name="rdf:type">
         <xsl:attribute name="rdf:resource">OHRM:ENTITY</xsl:attribute>
      </xsl:element>
   </xsl:template>

   <!--
        handle orhm's entity-function relationship (EFRSHIP table)
    -->
   <xsl:template name="handleOHRMFunction">
      <xsl:param name="entityId"/>
      <xsl:variable name="allFunctionforthisEntityId"
         select="ancestor::database/table[@name='EFRSHIP']/row/field[@name='EID' and text()=$entityId]/parent::node()"/>

      <xsl:for-each select="$allFunctionforthisEntityId">

         <xsl:if test="field[@name='EID']=$entityId">
            <xsl:variable name="fid" select="field[@name='FID']/text()"/>

            <xsl:element name="hasFunction">
               <xsl:attribute name="rdf:resource">
                  <xsl:value-of select="$fid"/>
               </xsl:attribute>
            </xsl:element>
            <!--
                    <xsl:element name="Function">

                    <xsl:variable name="fid" select="field[@name='FID']/text()"/>

                    <xsl:if test="$fid!=''">

                    <xsl:call-template name="handleFunction" >
                    <xsl:with-param name="fId" select="$fid"/>
                    </xsl:call-template>
                    </xsl:if>
                    </xsl:element>
                -->


         </xsl:if>
      </xsl:for-each>
   </xsl:template>

   <!--
        handle orhm's function table (FUNCTION table)
    -->
   <xsl:template name="handleFUNCTION">

      <xsl:if test="@name='FUNCTION'">
         <xsl:for-each select="row">
            <xsl:element name="rdf:Description">

               <xsl:variable name="fid" select="field[@name='FID']/text()"/>
               <xsl:call-template name="createRDFDescriptionAbout">
                  <xsl:with-param name="about" select="$fid"/>
               </xsl:call-template>

               <xsl:element name="rdf:type">
                  <xsl:attribute name="rdf:resource"
                     >OHRM:FUNCTION</xsl:attribute>
               </xsl:element>

               <xsl:for-each select="field">
                  <xsl:call-template name="genericCopyFromTable"/>
               </xsl:for-each>

            </xsl:element>
         </xsl:for-each>
      </xsl:if>

   </xsl:template>

   <!--
        create rdf Description's about attribute (EID field in ENTITY table)
    -->
   <xsl:template name="createRDFDescriptionAbout">
      <xsl:param name="about"/>
      <xsl:attribute name="rdf:about">
         <xsl:value-of select="translate($about,' ','_')"/>
      </xsl:attribute>

   </xsl:template>

   <!-- generic-->
   <xsl:template name="genericCopyFromTable">

      <xsl:variable name="fieldName">

         <xsl:choose>
            <xsl:when test="@name='ISBN/ISSN'">
               <xsl:value-of select="'ISBN_ISSN'"/>
            </xsl:when>
            <xsl:otherwise>
               <xsl:value-of select="@name"/>
            </xsl:otherwise>
         </xsl:choose>
      </xsl:variable>

      <xsl:element name="{$fieldName}">
         <xsl:copy-of select="text()"/>
      </xsl:element>


   </xsl:template>


   <!--
        create ohrm's relationship (RELATEDENTITY table)
    -->
   <xsl:template name="createRelatedEntity">
      <xsl:param name="entityId"/>

      <xsl:variable name="allRelatedEntityforthisEntityId"
         select="ancestor::database/table[@name='RELATEDENTITY']/row/field[@name='EID' and text()=$entityId]/parent::node()"/>

      <xsl:for-each select="$allRelatedEntityforthisEntityId">
         <xsl:variable name="relationshipType"
            select="field[@name='RERELATIONSHIP']/text()"/>
         <xsl:element name="{translate($relationshipType,' ','_')}">
            <xsl:attribute name="rdf:resource">
               <xsl:copy-of select="field[@name='REID']/text()"/>
            </xsl:attribute>
         </xsl:element>
      </xsl:for-each>
   </xsl:template>

   <!--
        handle ohrm's ENTITYNAME table
    -->
   <xsl:template name="createEntityName">
      <xsl:param name="entityId"/>

      <xsl:variable name="allEntityNameforthisEntityId"
         select="ancestor::database/table[@name='ENTITYNAME']/row/field[@name='EID' and text()=$entityId]/parent::node()"/>

      <xsl:for-each select="$allEntityNameforthisEntityId">

         <xsl:if test="field[@name='EID']=$entityId">
            <xsl:element name="hasEntityName">
               <xsl:attribute name="rdf:resource"><xsl:copy-of
                     select="$entityId"/>_<xsl:copy-of
                     select="translate(field[@name='ENALTERNATETYPE']/text(),' ','_')"
                     />_<xsl:copy-of
                     select="translate(field[@name='ENALTERNATE']/text(),' ','_')"
                  />
               </xsl:attribute>
               <!--
                        <xsl:attribute name="type"><xsl:copy-of select="field[@name='ENALTERNATETYPE']/text()"/></xsl:attribute>
                        <xsl:copy-of select="field[@name='ENALTERNATE']/text()"/>
                    -->
            </xsl:element>

         </xsl:if>
      </xsl:for-each>

   </xsl:template>


   <!--
        handle ohrm's Entity-publication relationship (EPRRSHIP table)
    -->
   <xsl:template name="handlePublication">

      <xsl:param name="entityId"/>
      <xsl:variable name="allPublicationforthisEntityId"
         select="ancestor::database/table[@name='EPRRSHIP']/row/field[@name='EID' and text()=$entityId]/parent::node()"/>

      <xsl:for-each select="$allPublicationforthisEntityId">

         <xsl:if test="field[@name='EID']=$entityId">
            <xsl:variable name="pubid" select="field[@name='PUBID']/text()"/>
            <xsl:element name="hasPublication">

               <xsl:attribute name="rdf:resource">
                  <xsl:value-of select="$pubid"/>
               </xsl:attribute>

            </xsl:element>

         </xsl:if>

      </xsl:for-each>
   </xsl:template>

   <!--
        handle ohrm's ENTITYNAME table
    -->
   <xsl:template name="handleENTITYNAME">

      <xsl:if test="@name='ENTITYNAME'">
         <xsl:for-each select="row">
            <xsl:element name="rdf:Description">

               <xsl:variable name="eId" select="field[@name='EID']/text()"/>
               <xsl:variable name="eType"
                  select="field[@name='ENALTERNATETYPE']/text()"/>
               <xsl:variable name="eValue"
                  select="field[@name='ENALTERNATE']/text()"/>
               <xsl:variable name="entityNameId"><xsl:copy-of select="$eId"
                     />_<xsl:copy-of select="$eType"/>_<xsl:copy-of
                     select="$eValue"/></xsl:variable>
               <xsl:call-template name="createRDFDescriptionAbout">
                  <xsl:with-param name="about" select="$entityNameId"/>
               </xsl:call-template>

               <xsl:for-each select="field">
                  <xsl:call-template name="genericCopyFromTable"/>
               </xsl:for-each>

            </xsl:element>
         </xsl:for-each>
      </xsl:if>

   </xsl:template>



   <!--
        handle ohrm's PUBRESOURCE table
    -->
   <xsl:template name="handlePUBRESOURCE">

      <xsl:if test="@name='PUBRESOURCE'">
         <xsl:for-each select="row">
            <xsl:element name="rdf:Description">

               <xsl:variable name="pubid" select="field[@name='PUBID']/text()"/>
               <xsl:call-template name="createRDFDescriptionAbout">
                  <xsl:with-param name="about" select="$pubid"/>
               </xsl:call-template>

               <xsl:element name="rdf:type">
                  <xsl:attribute name="rdf:resource"
                     >OHRM:PUBRESOURCE</xsl:attribute>
               </xsl:element>
               <xsl:for-each select="field">
                  <xsl:if test="@name!='Processing'">
                     <xsl:call-template name="genericCopyFromTable"/>
                  </xsl:if>
               </xsl:for-each>

            </xsl:element>
         </xsl:for-each>
      </xsl:if>

   </xsl:template>


   <!--
        handle ohrm's RELATIONSHIPS table
    -->
   <xsl:template name="handleRELATIONSHIPS">

      <xsl:if test="@name='RELATIONSHIPS'">
         <xsl:for-each select="row">
            <xsl:element name="rdf:Description">

               <xsl:variable name="rid"
                  select="field[@name='Relationship']/text()"/>
               <xsl:call-template name="createRDFDescriptionAbout">
                  <xsl:with-param name="about" select="$rid"/>
               </xsl:call-template>

               <xsl:element name="rdf:type">
                  <xsl:attribute name="rdf:resource"
                     >OHRM:RELATIONSHIPS</xsl:attribute>
               </xsl:element>
               <xsl:for-each select="field">
                  <xsl:call-template name="genericCopyFromTable"/>
               </xsl:for-each>

            </xsl:element>
         </xsl:for-each>
      </xsl:if>

   </xsl:template>





</xsl:stylesheet>
